<template>
  <div class="personalCenter-container">
    <div class="person-title">重置密码</div>
    <el-form class="person-form" ref="form" :model="form" label-width="80px">
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input v-model="retpassword" type="password" placeholder="请输入确认密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确认</el-button>
        <el-button @click="onCancle">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { retpassword } from "@/api/user.js"
  import { recordRoute } from "@/config/settings";
  export default {
    name: "PersonalCenter",
    data() {
      return {
        retpassword: '',
        form: {
          password: '',
        }
      };
    },
    created() {},
    methods: {
      async logout() {
        await this.$store.dispatch("user/logout");
        if (recordRoute) {
          const fullPath = this.$route.fullPath;
          this.$router.push(`/login?redirect=${fullPath}`);
        } else {
          this.$router.push("/login");
        }
      },
      onCancle() {
        this.form.password = '';
        this.retpassword = '';
      },
      onSubmit() {
        if(!this.form.password) {
          this.$message({
            message: "请输入密码",
            type: "warning",
          });
          return
        }
        if(!this.retpassword) {
          this.$message({
            message: "请输入确认密码",
            type: "warning",
          });
          return
        }
        if(this.form.password == this.retpassword) {
          this.$confirm("此操作将修改该账户密码, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              retpassword(this.form.password).then(() => {
                this.$message({
                  message: "修改密码成功，请使用新密码登录",
                  type: "success",
                });
                this.form.password = '';
                this.retpassword = '';
                this.logout();
              })
            })
        } else {
          this.$baseMessage("两次密码不相同");
        }
      }
    },
  };
</script>

<style lang="scss" scoped>
  .personalCenter-container {
    margin: 0 auto;
    .person-title{
      font-size: 24px;
      text-align: center;
      border-bottom: 1px solid #dfdfdf;
      padding-bottom: 30px;
    }
    .person-form{
      margin: 0 auto;
      width: 30%;
      margin-top: 30px;
    }
  }
</style>
